package com.attendance.www.hr.utils;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

public class TimeUtils {
	
	//计算时间间隔多少分钟
	public static Integer differMinutes(String fromTime,String toTime){
		String[] fromtimes = fromTime.split(":");
		String[] totimes = toTime.split(":");
		Integer minutes = Integer.parseInt(totimes[0]) * 60 + Integer.parseInt(totimes[1]) 
		- Integer.parseInt(fromtimes[0])*60 - Integer.parseInt(fromtimes[1]);
		if (minutes<0) {
			minutes=0;
		}
		return minutes;
	}
	
	//计算结束时间（小时：分钟）
	public static String getEndTime(String fromTime,Integer differMinutes){
		String[] fromtimes = fromTime.split(":");
		Integer endMinute = Integer.parseInt(fromtimes[0])*60 + Integer.parseInt(fromtimes[1]) + differMinutes;
		return String.valueOf(endMinute / 60) + ":" + String.valueOf(endMinute % 60);
	}
	
	//只取整数小时，多余的分钟舍弃
	public static Integer getHoursinterval(String fromTime,String toTime){
		String[] fromtimes = fromTime.split(":");
		String[] totimes = toTime.split(":");
		if(totimes[1].compareToIgnoreCase(fromtimes[1])>0){
			return Integer.parseInt(totimes[0])-Integer.parseInt(fromtimes[0]);
		}else{
			return Integer.parseInt(totimes[0])-Integer.parseInt(fromtimes[0])-1;
		}
	}

	//计算小时数，小数点后保留两位
	public static BigDecimal getHours(String fromDate,String fromTime,String toDate,String toTime){
		Calendar fromCalendar = Calendar.getInstance();
		Calendar toCalendar = Calendar.getInstance();
		String[] fromDates = fromDate.split("-");
		String[] toDates = toDate.split("-");
		String[] fromtimes = fromTime.split(":");
		String[] totimes = toTime.split(":");
		fromCalendar.set(Calendar.YEAR, Integer.parseInt(fromDates[0]));
		fromCalendar.set(Calendar.MONTH, Integer.parseInt(fromDates[1])-1);
		fromCalendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(fromDates[2]));
		fromCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(fromtimes[0]));
		fromCalendar.set(Calendar.MINUTE, Integer.parseInt(fromtimes[1]));
		toCalendar.set(Calendar.YEAR, Integer.parseInt(toDates[0]));
		toCalendar.set(Calendar.MONTH, Integer.parseInt(toDates[1])-1);
		toCalendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(toDates[2]));
		toCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(totimes[0]));
		toCalendar.set(Calendar.MINUTE, Integer.parseInt(totimes[1]));
		return new BigDecimal(toCalendar.getTimeInMillis()-fromCalendar.getTimeInMillis()).divide(new BigDecimal(1000*60*60), 2);
	}
	
	//计算小时数，小数点后保留两位
	public static BigDecimal getHours(Date fromDate,String fromTime,Date toDate,String toTime){
		Calendar fromCalendar = Calendar.getInstance();
		Calendar toCalendar = Calendar.getInstance();
		String[] fromtimes = fromTime.split(":");
		String[] totimes = toTime.split(":");
		fromCalendar.setTimeInMillis(fromDate.getTime());
		fromCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(fromtimes[0]));
		fromCalendar.set(Calendar.MINUTE, Integer.parseInt(fromtimes[1]));
		toCalendar.setTimeInMillis(toDate.getTime());
		toCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(totimes[0]));
		toCalendar.set(Calendar.MINUTE, Integer.parseInt(totimes[1]));
		return new BigDecimal(toCalendar.getTimeInMillis()-fromCalendar.getTimeInMillis()).divide(new BigDecimal(1000*60*60), 2);
	}
	
	/**
	 * 判断两个时间大小
	 * @param fromTime
	 * @param toTime
	 * @return 大于：1，小于：-1，相等：0
	 */
	public static Integer compareTime(String fromTime,String toTime){
		String[] fromtimes = fromTime.split(":");
		String[] totimes = toTime.split(":");
		if(Integer.parseInt(fromtimes[0])>Integer.parseInt(totimes[0])){
			return 1;
		}else if(Integer.parseInt(fromtimes[0])<Integer.parseInt(totimes[0])){
			return -1;
		}else{
			if(Integer.parseInt(fromtimes[1])>Integer.parseInt(totimes[1])){
				return 1;
			}else if(Integer.parseInt(fromtimes[1])<Integer.parseInt(totimes[1])){
				return -1;
			}else{
				return 0;
			}
		}
	}
	
	/**
	 * 判断两个时间大小
	 * @param fromTime
	 * @param toTime
	 * @return 大于：1，小于：-1，相等：0
	 */
	public static Integer compareDateTime(Date fromDate,String fromTime,Date toDate,String toTime){
		if(fromDate.compareTo(toDate)==0){
			String[] fromtimes = fromTime.split(":");
			String[] totimes = toTime.split(":");
			if(Integer.parseInt(fromtimes[0])>Integer.parseInt(totimes[0])){
				return 1;
			}else if(Integer.parseInt(fromtimes[0])<Integer.parseInt(totimes[0])){
				return -1;
			}else{
				if(Integer.parseInt(fromtimes[1])>Integer.parseInt(totimes[1])){
					return 1;
				}else if(Integer.parseInt(fromtimes[1])<Integer.parseInt(totimes[1])){
					return -1;
				}else{
					return 0;
				}
			}
		}else{
			return fromDate.compareTo(toDate);
		}

	}
	
	//计算时间间隔，分钟
	public static Integer getMinutes(Date fromDate,String fromTime,Date toDate,String toTime){
		Calendar fromCalendar = Calendar.getInstance();
		Calendar toCalendar = Calendar.getInstance();
		String[] fromtimes = fromTime.split(":");
		String[] totimes = toTime.split(":");
		fromCalendar.setTimeInMillis(fromDate.getTime());
		fromCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(fromtimes[0]));
		fromCalendar.set(Calendar.MINUTE, Integer.parseInt(fromtimes[1]));
		toCalendar.setTimeInMillis(toDate.getTime());
		toCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(totimes[0]));
		toCalendar.set(Calendar.MINUTE, Integer.parseInt(totimes[1]));
		
		return BigDecimal.valueOf((toCalendar.getTimeInMillis()-fromCalendar.getTimeInMillis()) / (1000*60)).intValue();
	}
	
	/**
	 * 判断time是否在from，to之内
	 * 
	 * @param time
	 *            指定日期
	 * @param from
	 *            开始日期
	 * @param to
	 *            结束日期
	 * @return
	 */
	public static boolean dateCalendar(Date time, Date from, Date to) {
		Calendar date = Calendar.getInstance();
		date.setTime(time);
		Calendar after = Calendar.getInstance();
		after.setTime(from);
		Calendar before = Calendar.getInstance();
		before.setTime(to);
		if (date.getTime().compareTo(after.getTime()) >= 0 && date.getTime().compareTo(before.getTime()) <= 0) {
			return true;
		} else {
			return false;
		}
	}
}
